import time
import curses

## High level Game classes

class game():
    def __init__(self, name, increment = 100):
        self.name = name
        self.timer = gametimer(increment)
        self.gamestack = [gamestate(self.timer)]

    def run(self):
        # run gamestate in window
        firstState = self.gamestack.pop()
        firstState.run()
        # exit game
        self.exit()


'''     self.display = gameDisplay(name + ": Game Window")
        self.display.createWindow()

    # Push a gamestate onto the game stack
    def pushGamestate(self, gamestate):
        self.gamestack.append(gamestate)

    # Pop a gamestate off of the gamestack
    def popGamestate(self, gamestate):
        gamestate = self.gamestack.pop()'''

'''
    # Set window parameters
    def setWindow(self, height, width, Color_1, Color_2):
        self.display.setwindow

    def createWindow(self, windth, height, Color_1, Color_2):
        self.display.createWindow(self, height, width, Color_1, Color_2)

    # Exit game
    def exit(self):
        self.window.close()
'''
# Game relevant classes
# Game state class - defines and stores one state
class gamestate():
    # Constructor
    def __init__(self, gametimer):
        self.inputstate = inputstate("keyboard controls")
        self.logicstate = logicstate("do nothing")
        self.renderstate = renderstate("print HAHA!")
        self.gobjectarray = []
        self.timer = gametimer

    # Start gamestate loop
    def run(self):
        self.timer.run()
        while True:
            timeralarm = False
            self.inputstate.run()
            self.logicstate.run()
            self.renderstate.run()
            while (timeralarm == False):
                timeralarm = self.timer.getalarm()

class substate():
    def __init__(self, name):
        # initialize
        self.name = name

    def run(self):
        #do nothing
        True

class inputstate(substate):
    def __init__(self, name):
        # init
        self.name = name

    def run(self):
        #do input
        print("get input")

class logicstate(substate):
    def __init__(self, name):
        # init
        self.name = name

    def run(self):
        # do nothing
        print("nothing happens")

class renderstate(substate):
    def __init__(self, name):
        # init
        self.name = name

    def run(self):
        # print
        print(" HAHA WORLD !!!!1!!1!11")
                
# Game timer - Used to maintain timing
class gametimer():
    def __init__(self, increment):
        self.increment = increment / 1000

    def run(self):
        self.starttime = time.time()
        self.stoptime = self.starttime + self.increment

    def getalarm(self):
        if (time.time() <= self.stoptime):
            return False
        else:
            self.starttime = self.stoptime
            self.stoptime = self.starttime + self.increment
            return True
'''
class gameDisplay():
    def __init__(self, name):
        self.name = name
        self.terminal = curses.initscr()
        curses.start_color()
        curses.noecho()
        curses.cbreak()
        self.terminal.keypad(1)

    def close(self):
        curses.nocbreak()
        self.terminal.keypad(0)
        curses.echo()
        curses.endwin()

    def createWindow(self, height, width, Color_1, Color_2):
        self.terminal.newwin(height, width, 0, 0)
#        if (curses.has_colors() and curses.can_change_color()):
#            print("hey")
            
#    def setWindow(self):


class gameobject():
    def __init__(self, name):
        self.name = name

class graphic():
    def __init__(self, name):
        self.name = name


if (__name__ == "__main__"):
    bob = game("bob", 100)
    bob.terminal.close()
'''
